package com.MSofLectures.logistic;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.MSofLectures.Entity.Lecture.Lecture;
import com.MSofLectures.Entity.Lecture.LectureImp;
import com.MSofLectures.Entity.User.UserImp;
import com.MSofLectures.Tool.StudentsInfoCollector;

public class Teacher extends Basic {
	private static final long serialVersionUID = 1L;
	/**
	 * @Title Teacher.java
	 * @Package com.logistic
	 * @author ufo
	 * 应付教师权限的web service
	 */
	private String[] lecture_keyName;
	private String[] lectureId;
	private String[] lecCodeNumber;
	private String[] shake;

	private String[] lecture_keyValue;
	private String[] lectureDescription;
	private String[] lecYear;
	private String[] enterYear;
	private String[] major;
	private String[] userId;
	private String[] stuentId;
	

	public String[] getStuentId() {
		return stuentId;
	}

	public void setStuentId(String[] stuentId) {
		this.stuentId = stuentId;
	}

	public String[] getUserId() {
		return userId;
	}

	public void setUserId(String[] userId) {
		this.userId = userId;
	}

	public String[] getMajor() {
		return major;
	}

	public void setMajor(String[] major) {
		this.major = major;
	}

	@SuppressWarnings("finally")
	public String listAllUsers() {
		try {
			if (isOnline()) {
				addSessionToUser();
				List<String> userList = this.user.listAllUsers();
				Integer i = 0;
				UserImp userTmp=null;
				for (String userId : userList) {
					userTmp = this.getOperatorOnUser().loadUid(userId);
					result.put("userName:" + i, userTmp.getValue("userName"));
					result.put("privilegeToCreateLecture:" + i,
							userTmp.getValue("privilegeToCreateLecture"));
					result.put("enterYear:" + i, userTmp.getValue("enterYear"));
					result.put("major:" + i, userTmp.getValue("major"));
					i++;
					userTmp.finalize();
				}
				result.put("amount", i.toString());// 注册总人数
				result.put("flag", i > 0 ? "true" : "false");
			}
		} catch (Exception e) {
			result.put("flag", "false");
			e.printStackTrace();
		} finally {
			return SUCCESS;
		}
	}

	@SuppressWarnings("finally")
	public String listAppointUsers() {
		try {
			if (isOnline()) {
				addSessionToUser();
				List<String> userList = null;
				if (getMajor()[0].equals("all")) {
					userList = this.user
							.listTheSameYearUsers(getEnterYear()[0]);
				} else {
					userList = this.user
							.listTheSameMajorUsers(getMajor()[0],getEnterYear()[0]);
				}
				Integer i = 0;
				UserImp userTmp=null;
				for (String userId : userList) {
					userTmp = this.getOperatorOnUser().loadUid(userId);
					result.put("userName" + i, userTmp.getValue("userName"));
					result.put("userId" + i, userTmp.getUserId());
					result.put("enterYear" + i, userTmp.getValue("enterYear"));
					result.put("major" + i, userTmp.getValue("major"));
					result.put("userclass" + i, userTmp.getValue("userclass"));
					i++;
					userTmp.finalize();
				}
				result.put("amount", i.toString());// 注册总人数
				result.put("flag", i > 0 ? "true" : "false");
				// result.put("flag", "true");
			}
		} catch (Exception e) {
			result.put("flag", "false");
			e.printStackTrace();
		} finally {
			return SUCCESS;
		}
	}

	public String showStudentsInfo() {
		try {
			if (isOnline()) {
				addSessionToUser();
				List<Map<String, String>> list = new StudentsInfoCollector()
						.collectStudentsInfo(getEnterYear()[0], getLecYear()[0]);
				Integer i = 0;
				for (Map<String, String> tmpStudent : list) {
					result.put("userId" + i, tmpStudent.get("userId"));
					result.put("countLectures" + i,
							tmpStudent.get("countLectures"));
					result.put("countCheckedLectures" + i,
							tmpStudent.get("countCheckedLectures"));
					result.put("countRegistedLectures" + i,
							tmpStudent.get("countRegistedLectures"));
					result.put("major" + i, tmpStudent.get("major"));
					result.put("enterYear" + i,
							tmpStudent.get("enterYear"));
					result.put("userName" + i, tmpStudent.get("userName"));
					result.put("userclass" + i, tmpStudent.get("userclass"));
					i++;
				}
				result.put("amount", i.toString());
				result.put("flag", i > 0 ? "true" : "false");
			}
		} catch (Exception e) {
			result.put("flag", "false");
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public void addSessionToUser() {// 将用户从session中取出，并检查是否具有Teacher权限
		this.user = getUser((String) request.getSession()
				.getAttribute("userId"), (String) request.getSession()
				.getAttribute("userPwd"));
		if (null == this.user.getValue("privilegeToCreateLecture"))
			this.user = null;
	}

	public String viewShakeResult() {
		try {
			if (isOnline()) {
				addSessionToUser();
				LectureImp lecturetmp = new Lecture(getLectureId()[0]);
				List<String> list = this.user.viewLectureShakeResult(
						getLectureId()[0], getShake()[0]);
				Iterator<String> i = list.iterator();
				int a = 0;
				UserImp user=null;
				for (; i.hasNext(); a++) {
					String tmp = i.next();
					result.put("userId" + a, tmp);

					user = this.getOperatorOnUser().loadUid(tmp);
					String order = user.userOrder(getLectureId()[0], tmp);
					result.put("username" + a, user.getValue("userName"));
					result.put("order" + a, order);
					try {
						user.finalize();
					} catch (Throwable e) {
						// TODO 自动生成的 catch 块
						e.printStackTrace();
					}
				}
				result.put("lecname", lecturetmp.getValue("lecName"));
				result.put("flag", a > 1 ? "true" : "false");
				try {
					lecturetmp.finalize();
				} catch (Throwable e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("flag", "false");
		}
		return SUCCESS;
	}

	public String shake() {
		try {
			if (isOnline()) {
				addSessionToUser();
				LectureImp lecture = this.getOperatorOnLecture().loadLecture(getLectureId()[0]);
				if (user.getUserId().equals(lecture.getLecture_submittorId())
						&& lecture.getValue("shake").equals("0")) {
					SynchronizedShaker shaker = new SynchronizedShaker(
							getLectureId()[0]);
					new Thread(shaker).start();
					lecture.altValue("shake", "1");
					lecture.save();
					result.put("flag", "true");// 只能证明执行成功，因为这里的shaker是多线程的
					try {
						lecture.finalize();
					} catch (Throwable e) {
						// TODO 自动生成的 catch 块
						e.printStackTrace();
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("flag", "false");
		}
		return SUCCESS;
	}

	public String showregisterCode() {
		try {
			if (isOnline()) {
				addSessionToUser();
				LectureImp lecturetmp = new Lecture(getLectureId()[0]);
				List<String> list = this.user
						.showLectureCode(getLectureId()[0]);
				Iterator<String> i = list.iterator();
				int a = 0;
				for (; i.hasNext(); a++)
					result.put("Code" + a, i.next());
				result.put("name", lecturetmp.getValue("lecName"));
				result.put("flag", a > 0 ? "true" : "false");
				try {
					lecturetmp.finalize();
				} catch (Throwable e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("flag", "false");
		}
		return SUCCESS;
	}

	public String showCheckResult() {
		try {
			if (isOnline()) {
				addSessionToUser();
				List<String> list = this.user.showCheck(getLectureId()[0]);

				Iterator<String> i = list.iterator();
				int a = 0;
				UserImp user=null;
				for (; i.hasNext(); a++) {
					String tmp = i.next();
					result.put("id" + a, tmp);
					user = this.getOperatorOnUser().loadUid(tmp);
					result.put("userName" + a, user.getValue("userName"));
					result.put("userId" + a, user.getUserId());
					result.put("enterYear" + a, user.getValue("enterYear"));
					result.put("major" + a, user.getValue("major"));
					result.put("userclass" + a, user.getValue("userclass"));
				
					try {
						user.finalize();
					} catch (Throwable e) {
						// TODO 自动生成的 catch 块
						e.printStackTrace();
					}
				}
				result.put("flag", a > 0 ? "true" : "false");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("flag", "false");
		}
		return SUCCESS;
	}

	public String addLecture() {// 添加讲座
		try {
			if (isOnline()) {
				addSessionToUser();
				LectureImp lecture = this.getOperatorOnLecture().newLecture(
						getLectureDescription()[0], this.user.getUserId());

				for (int i = 0; i < getLecture_keyName().length; i++)
					lecture.addKey(getLecture_keyName()[i],
							getLecture_keyValue()[i]);
				lecture.save();
				lecture.createRegisterCode(Integer.parseInt(lecture
						.getValue("lecCodeNumber")));

				result.put("lectureId", lecture.getLectureId());
				result.put("flag", "true");
				try {
					lecture.finalize();
				} catch (Throwable e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("flag", "false");
		}
		return SUCCESS;
	}

	public String altLecture() {
		try {
			addSessionToUser();
			if (this.user.getValue("privilegeToCreateLecture").equals("1")) {
				int i = 0;
				LectureImp lecturetmp=null;
				for (; i < getLectureId().length; i++) {
					lecturetmp = new Lecture(getLectureId()[i]);
					if (lecturetmp.getValue("lecCodeNumber").equals(
							getLecCodeNumber()[i])) {
						for (int j = 0; j < getLecture_keyName().length; j++)
							lecturetmp.altValue(getLecture_keyName()[j],
									getLecture_keyValue()[j]);
						lecturetmp.save();
						result.put("flag", "true");
					} else {

						lecturetmp.delete();
						LectureImp lecture = this.getOperatorOnLecture().newLecture("nothing",
								this.user.getUserId());

						for (int k = 0; k < getLecture_keyName().length; k++)
							lecture.addKey(getLecture_keyName()[k],
									getLecture_keyValue()[k]);
						lecture.save();
						lecture.createRegisterCode(Integer.parseInt(lecture
								.getValue("lecCodeNumber")));
						result.put("flag", "ok");
						try {
							lecture.finalize();
						} catch (Throwable e) {
							// TODO 自动生成的 catch 块
							e.printStackTrace();
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("flag", "false");
		}
		return SUCCESS;
	}

	public String deleteLecture() {
		try {
			int i = 0;
			addSessionToUser();
			String pri = this.user.getValue("privilegeToCreateLecture");
			LectureImp lecturetmp=null;
			for (; i < getLectureId().length; i++) {
				lecturetmp = new Lecture(getLectureId()[i]);
				if (pri.equals("1"))
					lecturetmp.delete();

				result.put("pri", pri);
				result.put("flag", "true");
				try {
					lecturetmp.finalize();
				} catch (Throwable e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("flag", "false");
		}
		return SUCCESS;
	}
	
	public String teacherRegisterLecture() {// 手动报名讲座
		try {
			addSessionToUser();
			if (this.getOperatorOnUser().isExist(getStuentId()[0])) {
				this.user.teacherRegisterLecture(getStuentId()[0],getLectureId()[0]);
				LectureImp lecturetmp = new Lecture(getLectureId()[0]);
				lecturetmp.createRegisterCode(1);
				result.put("flag", "true");
				
			}
			else{
				result.put("errormessage","true");
			}
				
			
		} catch (Exception e) {
			e.printStackTrace();
			result.put("flag", "false");
		}
		return SUCCESS;
	}
	

	public String[] getLecture_keyName() {
		return lecture_keyName;
	}

	public void setLecture_keyName(String[] lecture_keyName) {
		this.lecture_keyName = lecture_keyName;
	}

	public String[] getLecture_keyValue() {
		return lecture_keyValue;
	}

	public void setLecture_keyValue(String[] lecture_keyValue) {
		this.lecture_keyValue = lecture_keyValue;
	}

	@Override
	public Map<String, String> getResult() {
		// TODO 自动生成的方法存根
		return this.result;
	}

	public String[] getLectureDescription() {
		return lectureDescription;
	}

	public void setLectureDescription(String[] lectureDescription) {
		this.lectureDescription = lectureDescription;
	}

	public String[] getEnterYear() {
		return enterYear;
	}

	public void setEnterYear(String[] enterYear) {
		this.enterYear = enterYear;
	}

	public String[] getLecYear() {
		return lecYear;
	}

	public void setLecYear(String[] lecYear) {
		this.lecYear = lecYear;
	}

	public String[] getShake() {
		return shake;
	}

	public void setShake(String[] shake) {
		this.shake = shake;
	}

	public String[] getLecCodeNumber() {
		return lecCodeNumber;
	}

	public void setLecCodeNumber(String[] lecCodeNumber) {
		this.lecCodeNumber = lecCodeNumber;
	}

	public String[] getLectureId() {
		return lectureId;
	}

	public void setLectureId(String[] lectureId) {
		this.lectureId = lectureId;
	}
}
